﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;

namespace DominoPizzaRSSFeed
{
    public partial class Form_DominoRSS : Form
    {
        string m_connectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa; Password=sa";
        string m_rss_Feed8 = @"http://view.cpbadvertising.com/dominos/ooh/ribbon8.xml";
        string m_rss_Feed9 = @"http://view.cpbadvertising.com/dominos/ooh/ribbon9.xml";
        bool m_StartFetch = true;

        public Form_DominoRSS()
        {
            InitializeComponent();
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Bt_Start.Enabled = false;
            ThreadPool.QueueUserWorkItem(new WaitCallback(GetXmlFormat));
        }

        protected void GetXmlFormat(object o)
        {
            while (m_StartFetch)
            {
                string tradeFontStn = @"[Font Val=""TrdGthcLTStdBldCndNo20_Obl""]";
                string red = @"[Color Val=""#FF001F""]";
                string blue = @"[Color Val=""#0078AE""]";
                string white = @"[Color Val=""#FFFFFF""]";
                string imageTag = @"[DominoStar";

                string rss7Feed = @"[Font Val=""Trade Gothic LT Std Cn""]" + blue + "ORDER. TRACK. REVIEW. [DominoLogo]" + white + "DOMINOS.COM [DominoLogo] [b]";
                string rss8Feed = @"[Font Val=""Trade Gothic LT Std Cn""]";
                string rss9Feed = @"[Font Val=""Trade Gothic LT Std Cn""]";

                rss8Feed += red;
                rss9Feed += blue;

                XDocument x8Doc = XDocument.Load(m_rss_Feed8);
                IEnumerable<XElement> feedElements = x8Doc.Descendants("FEED");

                foreach (XElement el in feedElements)
                {
                    XElement starE = el.Element("Star");
                    int star = Convert.ToInt32(starE.Value);
                    XElement msgE = el.Element("MSG");
                    string msgTxt = msgE.Value;
                    msgTxt = msgTxt.Insert(msgTxt.LastIndexOf('-'), tradeFontStn);

                    rss8Feed += imageTag + star + "]" + msgTxt + "     ";
                }

                XDocument x9Doc = XDocument.Load(m_rss_Feed9);
                IEnumerable<XElement> feed9Elements = x9Doc.Descendants("FEED");

                foreach (XElement el in feed9Elements)
                {
                    XElement msgE = el.Element("MSG");
                    string msgTxt = msgE.Value;

                    msgTxt = msgTxt.Insert(msgTxt.IndexOf('-'), tradeFontStn);

                    rss9Feed += msgTxt + "     ";
                }

                SaveIntoDB(rss7Feed, rss8Feed, rss9Feed);

                Thread.Sleep(180000);
            }
        }

        protected void SaveIntoDB(string rss7Feed, string rss8Feed, string rss9Feed)
        {
            string update7RSSFeed = @"Update DataTag set LastValue=@LASTVALUE where [Name] = '[DominoRSS7]'";
            string update8RSSFeed = @"Update DataTag set LastValue=@LASTVALUE where [Name] = '[DominoRSS8]'";
            string update9RSSFeed = @"Update DataTag set LastValue=@LASTVALUE where [Name] = '[DominoRSS9]'";

            SqlConnection con = new SqlConnection(m_connectionString);
            SqlCommand rss7Command = new SqlCommand();
            rss7Command.CommandText = update7RSSFeed;
            rss7Command.CommandType = CommandType.Text;
            rss7Command.Connection = con;

            SqlCommand rss8Command = new SqlCommand();
            rss8Command.CommandText = update8RSSFeed;
            rss8Command.CommandType = CommandType.Text;
            rss8Command.Connection = con;

            SqlCommand rss9Command = new SqlCommand();
            rss9Command.CommandText = update9RSSFeed;
            rss9Command.CommandType = CommandType.Text;
            rss9Command.Connection = con;

            SqlParameter lastValue = new SqlParameter();
            lastValue.ParameterName = @"LASTVALUE";
            lastValue.SqlDbType = SqlDbType.VarChar;

            //RSS7
            lastValue.Size = rss7Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss7Feed;
            ExecuteQuery(rss7Command);

            //RSS8
            lastValue.Size = rss8Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss8Feed;
            ExecuteQuery(rss8Command);

            //RSS9
            lastValue.Size = rss8Feed.Length;
            lastValue.Direction = ParameterDirection.Input;
            lastValue.Value = rss8Feed;
            ExecuteQuery(rss9Command);
        }

        private void ExecuteQuery(SqlCommand command)
        {
            // Try to open the database and execute the update.
            try
            {
               // command.Connection.Open();
                //command.ExecuteNonQuery();
            }
            catch (Exception err)
            {
            }
            finally
            {
                //command.Connection.Close();
            }
        }

        private void Bt_Stop_Click(object sender, EventArgs e)
        {
            m_StartFetch = false;
            Bt_Start.Enabled = true;
        }

    }
}
